package tech.mhuang.pacebox.springboot.autoconfiguration.kaptcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.mhuang.pacebox.springboot.autoconfiguration.ConfigConsts;

import java.util.Properties;

import static com.google.code.kaptcha.Constants.KAPTCHA_BORDER;
import static com.google.code.kaptcha.Constants.KAPTCHA_BORDER_COLOR;
import static com.google.code.kaptcha.Constants.KAPTCHA_IMAGE_HEIGHT;
import static com.google.code.kaptcha.Constants.KAPTCHA_IMAGE_WIDTH;
import static com.google.code.kaptcha.Constants.KAPTCHA_NOISE_COLOR;
import static com.google.code.kaptcha.Constants.KAPTCHA_NOISE_IMPL;
import static com.google.code.kaptcha.Constants.KAPTCHA_OBSCURIFICATOR_IMPL;
import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_CONFIG_KEY;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE;
import static com.google.code.kaptcha.Constants.KAPTCHA_TEXTPRODUCER_IMPL;

/**
 * 验证码统一模板
 *
 * @author mhuang
 * @since 1.0.0
 */
@Configuration
@EnableConfigurationProperties(value = KaptchaProperties.class)
@ConditionalOnProperty(prefix = ConfigConsts.KAPTCHA, name = ConfigConsts.ENABLE, havingValue = ConfigConsts.TRUE)
public class KaptchaAutoConfiguration {

    private final KaptchaProperties properties;

    public KaptchaAutoConfiguration(KaptchaProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DefaultKaptcha kaptcha() {
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Properties prop = new Properties();
        Config config = new Config(prop);
        prop.setProperty(KAPTCHA_BORDER, properties.isBorderEnable() ? "yes" : "no");
        prop.setProperty(KAPTCHA_BORDER_COLOR, properties.getBorderColor());
        prop.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, properties.getTextFontColor());
        prop.setProperty(KAPTCHA_IMAGE_WIDTH, String.valueOf(properties.getImageWidth()));
        prop.setProperty(KAPTCHA_IMAGE_HEIGHT, String.valueOf(properties.getImageHeight()));
        prop.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, String.valueOf(properties.getTextFontSize()));
        prop.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptcha");
        prop.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, properties.getTextGenClazz());
        prop.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, String.valueOf(properties.getTextSpace()));
        prop.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, String.valueOf(properties.getTextLength()));
        prop.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, properties.getTextFontName());
        prop.setProperty(KAPTCHA_NOISE_COLOR, properties.getNoiseColor());
        prop.setProperty(KAPTCHA_NOISE_IMPL, properties.getNoiseGenClazz());
        prop.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, properties.getImageGenClazz());
        kaptcha.setConfig(config);
        return kaptcha;
    }
}
